// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import BaseView from "../base/BaseView";
import SeptkapaModule from "../module/SeptkapaModule";
import VirtualList from "../ui/VirtualList";
import { SearchConfigArgs } from "./items/SearchConfigItem";

const {ccclass, property} = cc._decorator;

var OperateType = {
    Save: 'save',
    Select: 'select',
};

const SearchConfigListKey = 'search_config_list_key';
const SearchConfigKey = 'search_config';

@ccclass
export default class SearchConfigView extends BaseView {

    @property(cc.Button)
    btnConfirm: cc.Button = null;

    @property(cc.EditBox)
    editbox: cc.EditBox = null;

    @property(VirtualList)
    vritualList: VirtualList<SearchConfigArgs> = null;

    type: string = 'select';

    _callback: any = null;

    _config: any = null;
    // LIFE-CYCLE CALLBACKS:

    onLoad () {

    }

    show(params?: Object): void {
        this.type = params['type'];
        if (this.type == OperateType.Select) {
            this.editbox.node.active = false;
            this.btnConfirm.node.active = false;
            this._callback = params['callback'];
        } else if (this.type == OperateType.Save) {
            this.editbox.node.active = true;
            this.btnConfirm.node.active = true;
            this.editbox.string = '';
            this._config = params['config'];
        }
        this.updateList();
    }

    onBtnConfirm () {
        if (this.editbox.string != '') {
            let key = this.editbox.string;
            SeptkapaModule.getInstance().setValue(key, this._config);
            let list = this.getConfigList();
            let isFind = false;
            for (let index = 0; index < list.length; index++) {
                const name = list[index];
                if (name == key) {
                    isFind = true;
                    break;
                }
            }
            if (isFind == false) {
                list.push(key);
                SeptkapaModule.getInstance().setValue(SearchConfigListKey, list);
            }
            this.hide();
        } else {
            this.showToast('请输入保存名称');
        }
    }

    private getConfigList () {
        let list = SeptkapaModule.getInstance().getValue(SearchConfigListKey);
        if (list == null) {
            list = [];
        }
        return list.slice();
    }

    updateList () {
        let list = this.getConfigList();
        list.unshift('上次搜索');
        let args = [];
        for (let index = 0; index < list.length; index++) {
            const name = list[index];
            let itemData = {
                name: name,
                clickCallback: (index : number) => {
                    this.onClickItem(index);
                },
                deleteCallback: (index : number) => {
                    this.onDeleteItem(index);
                },
            };
            args.push(itemData);
        }
        this.vritualList.argsArr = args;
    }

    onClickItem (index: number) {
        if (this.type == OperateType.Save) {
            if (index == 0) {
                this.showToast('默认配置不能修改');
                return;
            }
            let list = this.getConfigList();
            this.editbox.string = list[index - 1];
        } else if (this.type == OperateType.Select) {
            let name = '';
            if (index == 0) {
                name = SearchConfigKey;
            } else {
                let list = this.getConfigList();
                name = list[index - 1];
            }
            let config = SeptkapaModule.getInstance().getValue(name);
            if (config) {
                this._callback(config);
            }
        }
    }

    onDeleteItem (index: number) {
        if (index == 0) {
            this.showToast('默认配置不能修改');
            return;
        }

        let list = this.getConfigList();
        list.splice(index - 1, 1);
        SeptkapaModule.getInstance().setValue(SearchConfigListKey, list);
        this.updateList();
    }
    // update (dt) {}
}
